package org.poem.service.impl;


import org.poem.StringUtils;
import org.poem.datasource.DataSourceVo;
import org.poem.exception.CanalClientException;
import org.poem.service.DatasourceTest;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sangfor
 */
public class AbstractDatasource implements DatasourceTest {

    /**
     * 测试连接
     */
    private static Map<String, Class> datasourceMap = new HashMap<String, Class>() {{
        put("MySQL", MysqlDatasource.class);
    }};

    public static Map<String, Class> getDatasourceMap() {
        return datasourceMap;
    }

    /**
     * 数据测试
     *
     * @param dataSource      数据源信息
     * @throws CanalClientException 出现问题
     */
    @Override
    public void testSource(DataSourceVo dataSource) throws CanalClientException {

        if (dataSource == null) {
            throw new CanalClientException("数据库信息不能为空");
        }
        String userName = dataSource.getUserName();
        String password = dataSource.getPassword();
        String ip = dataSource.getIp();
        Integer port = dataSource.getPort();
        if (StringUtils.isEmpty(userName)) {
            throw new CanalClientException("用户名不能为空");
        }
        if (StringUtils.isEmpty(ip)) {
            throw new CanalClientException("ip不能为空");
        }
        if (port == null || port < -1) {
            throw new CanalClientException("端口号不能为空，并且大于0");
        }
        if (StringUtils.isEmpty(password)) {
            throw new CanalClientException("密码不能为空.");
        }
    }
}
